﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace DSLOrder
{
    public partial class selectsp : Form
    {
        public selectsp()
        {
            InitializeComponent();
        }

        MsSqlHelper mssql = new MsSqlHelper();
        Common Init = new Common();
        public string nationalMedicine=string.Empty;
        public string manufacturer = string.Empty;
        public string supplierItemId = string.Empty;
        public string itemId = string.Empty;
        public string itemName = string.Empty;
        public string ypgg = string.Empty;
        public string dw = string.Empty;

        private void selectsp_Load(object sender, EventArgs e)
        {
            PublicClass.Initialize();
            tbSearch.Text = nationalMedicine;

            txtnationalMedicine.Text = nationalMedicine;
            txtmanufacturer.Text = manufacturer;
            txtitemId.Text = itemId;
            txtitemName.Text = itemName;
            txtypgg.Text = ypgg;
            txtdw.Text = dw;
            string con = tbSearch.Text;
            /*
            try
            {
                string sql="select top 100 rtrim(spid) as spid,rtrim(spbh) as spbh,rtrim(spmch) as spmch,rtrim(shpgg) as shpgg,rtrim(dw) as dw,rtrim(shengccj) as shengccj,rtrim(pizhwh) as pizhwh from [spkfk](nolock) where beactive='是' and (pizhwh like '%"+con+"%' or spbh like '%" + con + "%' or spmch like '%" + con + "%' or zjm like '%" + con + "%') order by spbh";
                DataSet ds = mssql.ExecuteDs(sql);
                txtinfo.Text = sql;
                if (ds.Tables[0].Rows.Count > 0)
                {
                    dataGridView1.DataSource = ds.Tables[0];
                    //AutoData();
                }


            }
            catch (Exception ex)
            {
                Init.WriteLog(ex.ToString(), true);
            }
             */
        }
        protected override bool ProcessCmdKey(ref System.Windows.Forms.Message msg, System.Windows.Forms.Keys keyData)
        {
            int WM_KEYDOWN = 256;
            int WM_SYSKEYDOWN = 260;
            if (msg.Msg == WM_KEYDOWN | msg.Msg == WM_SYSKEYDOWN)
            {
                switch (keyData)
                {
                    case Keys.Escape:
                        this.Close();//esc关闭窗体
                        break;
                }
            }
            return false;
        }
        protected override void OnKeyDown(KeyEventArgs e)
        {
            if (e.KeyData == Keys.Enter)
            {
                int col = dataGridView1.CurrentCell.ColumnIndex;
                int row = dataGridView1.CurrentCell.RowIndex;
                if (row != dataGridView1.NewRowIndex)
                {
                    if (col == (dataGridView1.Columns.Count - 1))
                    {
                        col = -1;
                        row++;
                    }
                    dataGridView1.CurrentCell = dataGridView1[col + 1, row];
                }
                e.Handled = true;
            }
            base.OnKeyDown(e);
        }

        

        private void dataGridView1_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyData == Keys.Enter || e.KeyData == Keys.Return)
            {                
                int row = this.dataGridView1.CurrentCell.RowIndex;//选定当前选定的单元格所处的行号
                int col = this.dataGridView1.CurrentCell.ColumnIndex;//选定当前选定的单元格所处的列号
                string supplierItemId = dataGridView1.Rows[row].Cells[0].Value.ToString();//将单元格的内容显示到文本
                if (!string.IsNullOrEmpty(supplierItemId))
                {
                    PublicClass.Instance.spid = supplierItemId;
                    SetString(itemId, supplierItemId);
                }               
                this.Close();
                this.Dispose();
            }
        }
        private void dataGridView1_CellMouseDoubleClick(object sender, DataGridViewCellMouseEventArgs e)
        {
            int row = this.dataGridView1.CurrentCell.RowIndex;//选定当前选定的单元格所处的行号
            int col = this.dataGridView1.CurrentCell.ColumnIndex;//选定当前选定的单元格所处的列号
            string supplierItemId = dataGridView1.Rows[row].Cells[0].Value.ToString();//将单元格的内容显示到文本
            if (!string.IsNullOrEmpty(supplierItemId))
            {
                PublicClass.Instance.spid = supplierItemId;
                SetString(itemId, supplierItemId);
            }           
            this.Close();
            this.Dispose();
        }

        private void tbSearch_TextChanged(object sender, EventArgs e)
        {
            try
            {
                dataGridView1.RowHeadersVisible = false;
                string con = tbSearch.Text;               
                string sql = "select top 100 rtrim(spid) as spid,rtrim(spbh) as spbh,rtrim(spmch) as spmch,rtrim(shpgg) as shpgg,rtrim(dw) as dw,rtrim(shengccj) as shengccj,rtrim(pizhwh) as pizhwh from [spkfk](nolock) where beactive='是' and (pizhwh like '%" + con + "%' or spbh like '%" + con + "%' or spmch like '%" + con + "%' or zjm like '%" + con + "%') order by spbh";
                DataTable dt = mssql.ExecuteDt(sql);
                txtinfo.Text = sql;
                if (dt.Rows.Count>0)
                {                    
                    dataGridView1.DataSource = dt;
                    AutoData();
                }
            }
            catch (Exception ex)
            {
                Init.WriteLog(ex.ToString(), true);
            }
        }

        public void SetString(string itemId,string supplierItemId)
        {
            string sql ="";
             sql = "select top 1 1 from [csc_user_bind_goods] where itemId='" + itemId + "' and supplierItemId='" + supplierItemId + "'";
           if(!mssql.Getbool(sql))
           {
               sql = "insert [csc_user_bind_goods](itemId,supplierItemId)values('" + itemId + "','" + supplierItemId + "') ";
               if(mssql.ExecuteSql(sql)==0){
                   MessageBox.Show("更新关联关系失败！");
               }
           }
           else
           {
               MessageBox.Show("更新关联关系失败！\r\n已经存在关联关系！");
           }
        }

        private void AutoData()
        {
            Common.AutoSizeColumn(dataGridView1);
            dataGridView1.Columns["spid"].HeaderText = "ID";
            dataGridView1.Columns["spid"].ToolTipText = "[只读列]";
            dataGridView1.Columns["spid"].Visible = false;
            dataGridView1.Columns["spbh"].HeaderText = "商品编号";
            dataGridView1.Columns["spmch"].HeaderText = "商品名称";
            dataGridView1.Columns["shpgg"].HeaderText = "商品规格";
            dataGridView1.Columns["dw"].HeaderText = "单位";
            dataGridView1.Columns["shengccj"].HeaderText = "生产厂家";
            dataGridView1.Columns["pizhwh"].HeaderText = "批准文号";
        }

        private void btnCancelX_Click(object sender, EventArgs e)
        {            
            string sql ="";
             sql = "select top 1 1 from [csc_user_bind_goods] where itemId='" + itemId + "' and supplierItemId='" + supplierItemId + "'";             
           if(mssql.Getbool(sql))
           {
               sql = "delete from [csc_user_bind_goods] where itemId='" + itemId + "' and supplierItemId='" + supplierItemId + "'";
               if(mssql.ExecuteSql(sql)>0){
                   MessageBox.Show("关系删除成功！");
                   PublicClass.Instance.spid = "";
                   this.Close();
                   this.Dispose();
               }
           }
           else
           {
               MessageBox.Show("更新关联关系失败！");
           }
             
        }
    }
}
